package com.gh.manager.clazz.controller;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.gh.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gh.common.annotation.Log;
import com.gh.common.core.controller.BaseController;
import com.gh.common.core.domain.AjaxResult;
import com.gh.common.enums.BusinessType;
import com.gh.manager.clazz.domain.GhClazz;
import com.gh.manager.clazz.service.IGhClazzService;
import com.gh.common.utils.poi.ExcelUtil;
import com.gh.common.core.page.TableDataInfo;

/**
 * 班级Controller
 * 
 * @author andas
 * @date 2024-12-26
 */
@RestController
@RequestMapping("/manager/clazz")
public class GhClazzController extends BaseController
{
    @Autowired
    private IGhClazzService ghClazzService;

    /**
     * 为班级分配学员
     * @return
     */
    @PreAuthorize("@ss.hasPermi('manager:clazz:add')")
    @PostMapping("/assign")
    public AjaxResult assignPlayer(@RequestBody Map<String, Object> params) {
        // 获取 userIds 列表并进行类型转换
        List<Integer> intIds = (List<Integer>) params.get("userIds");
        List<Long> userIds = intIds.stream()
                .map(Long::valueOf)
                .collect(Collectors.toList());

        Long clazzId = Long.valueOf(params.get("clazzId").toString());
        String success = ghClazzService.assignPlayer(userIds, clazzId);
        return success();
    }


    /**
     * 查询班级详情
     */
    @PreAuthorize("@ss.hasPermi('manager:clazz:list')")
    @GetMapping("/details")
    public TableDataInfo clazzDetails(GhClazz ghClazz)
    {
        List<SysUser> list = ghClazzService.selectClazzDetails(ghClazz);
        return getDataTable(list);
    }

    /**
     * 查询班级列表
     */
    @PreAuthorize("@ss.hasPermi('manager:clazz:list')")
    @GetMapping("/list")
    public TableDataInfo list(GhClazz ghClazz)
    {
        startPage();
        List<GhClazz> list = ghClazzService.selectGhClazzList(ghClazz);
        return getDataTable(list);
    }

    /**
     * 导出班级列表
     */
    @PreAuthorize("@ss.hasPermi('manager:clazz:export')")
    @Log(title = "班级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GhClazz ghClazz)
    {
        List<GhClazz> list = ghClazzService.selectGhClazzList(ghClazz);
        ExcelUtil<GhClazz> util = new ExcelUtil<GhClazz>(GhClazz.class);
        util.exportExcel(response, list, "班级数据");
    }

    /**
     * 获取班级详细信息
     */
    @PreAuthorize("@ss.hasPermi('manager:clazz:query')")
    @GetMapping(value = "/{clazzId}")
    public AjaxResult getInfo(@PathVariable("clazzId") Long clazzId)
    {
        return success(ghClazzService.selectGhClazzByClazzId(clazzId));
    }

    /**
     * 新增班级
     */
    @PreAuthorize("@ss.hasPermi('manager:clazz:add')")
    @Log(title = "班级", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GhClazz ghClazz)
    {
        return toAjax(ghClazzService.insertGhClazz(ghClazz));
    }

    /**
     * 修改班级
     */
    @PreAuthorize("@ss.hasPermi('manager:clazz:edit')")
    @Log(title = "班级", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GhClazz ghClazz)
    {
        return toAjax(ghClazzService.updateGhClazz(ghClazz));
    }

    /**
     * 删除班级
     */
    @PreAuthorize("@ss.hasPermi('manager:clazz:remove')")
    @Log(title = "班级", businessType = BusinessType.DELETE)
	@DeleteMapping("/{clazzIds}")
    public AjaxResult remove(@PathVariable Long[] clazzIds)
    {
        return toAjax(ghClazzService.deleteGhClazzByClazzIds(clazzIds));
    }
}
